﻿using System;
using System.Linq;
using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Swtor;
using WingIt.Dynamics;
using Action = Buddy.BehaviorTree.Action;

namespace WingIt.Routines
{
    public static class MercenaryArsenal
    {

        [Class(CharacterClass.BountyHunter, AdvancedClass.Mercenary, SkillTreeId.MercenaryArsenal)]
        [Behavior(BehaviorType.Pull)]
        public static Composite MercenaryArsenalPull()
        {
            return new PrioritySelector(
                // new Decorator(ret => BuddyTor.Me.IsMounted, new PrioritySelector()), --- still pulling if mounted
                // We need to sort our movement and line-of-sight stuff out before anything else.
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 2.8f),
                Movement.StopInRange(2.8f),
                Spell.BuffSelf("Thermal Sensor Override", ret => BuddyTor.Me.Level > 41 && AbilityManager.HasAbility("Thermal Sensor Override")),
                Spell.Cast("Fusion Missile", ret => BuddyTor.Me.Level > 17 && AbilityManager.HasAbility("Fusion Missile")),
                Spell.Cast("Explosive Dart", ret => AbilityManager.HasAbility("Explosive Dart")),
                Spell.Cast("Rapid Shots", ret => true)
                );

        }

        [Behavior(BehaviorType.Combat)]
        [Class(CharacterClass.BountyHunter, AdvancedClass.Mercenary, SkillTreeId.MercenaryArsenal)]
        public static Composite MercenaryArsenalCombat()
        {
            return new PrioritySelector(
                // We need to sort our movement and line-of-sight stuff out before anything else.
                Movement.StopInRange(2.8f),
                #region Lazyraiderish stuff - need to comment out movment stuff for this to work
                new Decorator(ret => BuddyTor.Me.IsMoving,
                    new PrioritySelector(
                        Spell.BuffSelf("Energy Shield", ret => BuddyTor.Me.HealthPercent < 40),
                        Spell.Cast("Kolto Overload", ret => BuddyTor.Me.HealthPercent <= 60),
                        Spell.Cast("Rapid Shots", ret => BuddyTor.Me.ResourceStat > 30),
                        Spell.Cast("Jet Boost", ret => BuddyTor.Me.ResourceStat <= 80 && Helpers.Targets.Count(t => t.Distance <= .4f) >= 2),
                        Spell.Cast("Heatseeker Missiles", ret => BuddyTor.Me.ResourceStat <= 80),
                        Spell.Cast("Rail Shot", ret => true),
                        Spell.Cast("Explosive Dart", ret => true),
                        Spell.Cast("Rapid Shots", ret => true)
                    )
                ),
            #endregion
                Spell.Cast("Tracer Missile", ret => BuddyTor.Me.ResourceStat <= 24 && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Heat Signature" && B.Caster.Name == BuddyTor.Me.Name) != null && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Heat Signature" && B.Caster.Name == BuddyTor.Me.Name).TimeLeft.Seconds < 6),
                Spell.WaitForCast(),
                Spell.Cast("Determination", ret => BuddyTor.Me.IsStunned),
                Spell.BuffSelf("Energy Shield", ret => BuddyTor.Me.HealthPercent <= 40),
                Spell.Cast("Kolto Overload", ret => BuddyTor.Me.HealthPercent <= 60),
                Spell.Cast("Unload", ret => BuddyTor.Me.ResourceStat <= 24 && BuddyTor.Me.HasBuff("Barrage")),
                Spell.Cast("Rapid Shots", ret => BuddyTor.Me.ResourceStat > 30),
                Spell.Cast("Jet Boost", ret => BuddyTor.Me.ResourceStat <= 24 && Helpers.Targets.Count(t => t.Distance <= .4f) >= 2),
                Spell.Cast("Rocket Punch", ret => BuddyTor.Me.ResourceStat <= 24 && BuddyTor.Me.CurrentTarget.Distance <= .8f),
                new Decorator(ret => !AbilityManager.HasAbility("Tracer Missile"),
                    new PrioritySelector(
                        Spell.Cast("Explosive Dart", castWhen => Helpers.Targets.Count() >= 3 && BuddyTor.Me.ResourceStat <= 24),
                        Spell.Cast("Rail Shot", ret => true),
                        Spell.Cast("Power Shot", ret => AbilityManager.CanCast("Power Shot", BuddyTor.Me.CurrentTarget)),
                        Spell.Cast("Missile Blast", ret => BuddyTor.Me.ResourceStat <= 24),
                        Spell.Cast("Rapid Shots", ret => true)
                    )
                ),
                Spell.Cast("Heatseeker Missiles", ret => BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Heat Signature" && B.Caster.Name == BuddyTor.Me.Name) != null && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Heat Signature" && B.Caster.Name == BuddyTor.Me.Name).Stacks > 3 && BuddyTor.Me.ResourceStat <= 80),
                Spell.Cast("Rail Shot", ret => BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Heat Signature" && B.Caster.Name == BuddyTor.Me.Name) != null && BuddyTor.Me.CurrentTarget.Debuffs.FirstOrDefault(B => B.Name == "Heat Signature" && B.Caster.Name == BuddyTor.Me.Name).Stacks > 3 && BuddyTor.Me.ResourceStat <= 80 && BuddyTor.Me.HasBuff("Tracer Lock") && BuddyTor.Me.Buffs.FirstOrDefault(B => B.Name == "Tracer Lock").Stacks > 2),
                Spell.Cast("Vent Heat", ret => BuddyTor.Me.ResourceStat > 80),
                Spell.Cast("Tracer Missile", ret => BuddyTor.Me.ResourceStat <= 24),
                Spell.Cast("Rapid Shots", ret => true),
                Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 2.8f)
                );
        }

        [Class(CharacterClass.BountyHunter, AdvancedClass.Mercenary, SkillTreeId.MercenaryArsenal)]
        [Behavior(BehaviorType.OutOfCombat)]
        public static Composite MercenaryArsenalOutOfCombat()
        {
            return new PrioritySelector(
                Spell.Cast("Recharge and Reload", ret => BuddyTor.Me.ResourceStat >= 50 || BuddyTor.Me.HealthPercent < 100),
                Spell.BuffSelf("High Velocity Gas Cylinder", ret => AbilityManager.HasAbility("High Velocity Gas Cylinder")),
                Spell.BuffSelf("Combustible Gas Cylinder", ret => !AbilityManager.HasAbility("High Velocity Gas Cylinder")),
                Spell.WaitForCast()
                );
        }
    }
}
